package util1;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {
	
	public static final SessionFactory sessionFactory;
	public static Session session;
	
	static{
		try{
			/*sessionFactory = new AnnotationConfiguration().configure(
					"hibernate.cfg.xml").buildSessionFactory();*/
			Configuration configuracion = new Configuration();
			configuracion.configure();
			ServiceRegistry registro = new ServiceRegistryBuilder().applySettings(configuracion
					.getProperties()).buildServiceRegistry();
			sessionFactory = configuracion.buildSessionFactory(registro);
		} catch(Throwable ex){
			System.err.println("Initial SessionFactory creation failed. "+ex);
			throw new ExceptionInInitializerError(ex);
		}
	}
	
	public static SessionFactory getSessionFactory(){
		return sessionFactory;
	}

	public static void iniciarSesion(){
		session = sessionFactory.openSession();
	}
	
	public static void cerrarSesion(){
		try{
			session.close();
		} catch(Exception ex){
			System.out.println(ex.getMessage());
		}
	}
}
